package com.flyco.modules.ums.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.sms.entity.SmsAddbuyInfo;
import com.flyco.modules.sms.vo.SmsAddbuyInfoPage;
import com.flyco.modules.ums.entity.UmsMemberStaff;
import com.flyco.modules.ums.entity.UmsStaffEntranceGuard;
import com.flyco.modules.ums.service.IUmsMemberStaffService;
import com.flyco.modules.ums.service.IUmsStaffEntranceGuardService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

 /**
 * @Description: 员工通行状态
 * @Author: flyco
 * @Date:   2022-03-24
 * @Version: V1.0
 */
@Slf4j
@Api(tags="员工通行状态")
@RestController
@RequestMapping("/f/ums/umsStaffEntranceGuard")
public class UmsStaffEntranceGuardController extends BaseController<UmsStaffEntranceGuard, IUmsStaffEntranceGuardService> {
	@Autowired
	private IUmsStaffEntranceGuardService umsStaffEntranceGuardService;
	@Autowired
	private IUmsMemberStaffService umsMemberStaffService;
	
	/**
	 * 分页列表查询
	 *
	 * @param umsStaffEntranceGuard
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "员工通行状态-分页列表查询")
	@ApiOperation(value="员工通行状态-分页列表查询", notes="员工通行状态-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(UmsStaffEntranceGuard umsStaffEntranceGuard,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<UmsStaffEntranceGuard> queryWrapper = QueryGenerator.initQueryWrapper(umsStaffEntranceGuard, req.getParameterMap());
		Page<UmsStaffEntranceGuard> page = new Page<UmsStaffEntranceGuard>(pageNo, pageSize);
		IPage<UmsStaffEntranceGuard> pageList = umsStaffEntranceGuardService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 * 添加
	 *
	 * @param umsStaffEntranceGuard
	 * @return
	 */
	@AutoLog(value = "员工通行状态-添加")
	@ApiOperation(value="员工通行状态-添加", notes="员工通行状态-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody UmsStaffEntranceGuard umsStaffEntranceGuard) {
		if (oConvertUtils.isEmpty(umsStaffEntranceGuard) || oConvertUtils.isEmpty(umsStaffEntranceGuard.getMobile()))
			return Result.error("手机号不可为空");
		LambdaQueryWrapper<UmsMemberStaff> staffQueryWrapper = new LambdaQueryWrapper<>();
		staffQueryWrapper.eq(UmsMemberStaff::getMobile,umsStaffEntranceGuard.getMobile());
		UmsMemberStaff staff = umsMemberStaffService.getOne(staffQueryWrapper,false);
		if (oConvertUtils.isEmpty(staff)) {
			staff = new UmsMemberStaff();
			staff.setStaffNo(umsStaffEntranceGuard.getStaffNo());
			staff.setCompany(umsStaffEntranceGuard.getCompany());
			staff.setDepartment(umsStaffEntranceGuard.getDepartment());
			staff.setName(umsStaffEntranceGuard.getName());
			staff.setMobile(umsStaffEntranceGuard.getMobile());
			umsMemberStaffService.save(staff);
			umsStaffEntranceGuard.setId(staff.getId());
		} else {
			umsStaffEntranceGuard.setId(staff.getId());
		}
		umsStaffEntranceGuardService.saveOrUpdate(umsStaffEntranceGuard);
		return Result.ok("添加成功！");
	}
	
	/**
	 * 编辑
	 *
	 * @param umsStaffEntranceGuard
	 * @return
	 */
	@AutoLog(value = "员工通行状态-编辑")
	@ApiOperation(value="员工通行状态-编辑", notes="员工通行状态-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody UmsStaffEntranceGuard umsStaffEntranceGuard) {
		umsStaffEntranceGuardService.updateById(umsStaffEntranceGuard);
		return Result.ok("编辑成功!");
	}
	
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工通行状态-通过id删除")
	@ApiOperation(value="员工通行状态-通过id删除", notes="员工通行状态-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		umsStaffEntranceGuardService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "员工通行状态-批量删除")
	@ApiOperation(value="员工通行状态-批量删除", notes="员工通行状态-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.umsStaffEntranceGuardService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "员工通行状态-通过id查询")
	@ApiOperation(value="员工通行状态-通过id查询", notes="员工通行状态-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		UmsStaffEntranceGuard umsStaffEntranceGuard = umsStaffEntranceGuardService.getById(id);
		return Result.ok(umsStaffEntranceGuard);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param umsStaffEntranceGuard
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, UmsStaffEntranceGuard umsStaffEntranceGuard) {
      return super.exportXls(request, umsStaffEntranceGuard, UmsStaffEntranceGuard.class, "员工通行状态");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<UmsStaffEntranceGuard> list = ExcelImportUtil.importExcel(file.getInputStream(), UmsStaffEntranceGuard.class, params);
              for (UmsStaffEntranceGuard umsStaffEntranceGuard : list) {
                  if (oConvertUtils.isNotEmpty(umsStaffEntranceGuard.getMobile())) {
                      LambdaQueryWrapper<UmsMemberStaff> staffQueryWrapper = new LambdaQueryWrapper<>();
                      staffQueryWrapper.eq(UmsMemberStaff::getMobile,umsStaffEntranceGuard.getMobile());
                      UmsMemberStaff staff = umsMemberStaffService.getOne(staffQueryWrapper,false);
                      if (oConvertUtils.isEmpty(staff)) {
                          staff = new UmsMemberStaff();
                          staff.setStaffNo(umsStaffEntranceGuard.getStaffNo());
                          staff.setCompany(umsStaffEntranceGuard.getCompany());
                          staff.setDepartment(umsStaffEntranceGuard.getDepartment());
                          staff.setName(umsStaffEntranceGuard.getName());
                          staff.setMobile(umsStaffEntranceGuard.getMobile());
                          umsMemberStaffService.save(staff);
                          umsStaffEntranceGuard.setId(staff.getId());
                      } else {
                          umsStaffEntranceGuard.setId(staff.getId());
                      }
                      umsStaffEntranceGuardService.saveOrUpdate(umsStaffEntranceGuard);
                  }
              }
              return Result.ok("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(), e);
              return Result.error("文件导入失败:" + e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败！");
  }

}
